package com.Mobile.Action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.Entity.Community;
import com.Entity.Userdata;
import com.Entity.Users;
import com.Service.CommunityService;
import com.Service.UserdataService;
import com.Service.UsersService;

public class UpdateCommonProfileAction implements ServletResponseAware {
	 //更新个人资料(医生)
	private HttpServletResponse response;//response闁诲海鏁搁、濠囨寘?
	private int flag=0;
	@Autowired
	private UsersService usersservice;
	@Autowired
	private UserdataService userdataservice;
	@Autowired
	private CommunityService communityservice;
	private Users user=new Users();
	private Userdata userdata=new Userdata();
	private Community community=new Community();
	
	public void UpdateProfile() throws ServletException,IOException {
		
		String userid=ServletActionContext.getRequest().getParameter("UserID");
		String nickname=ServletActionContext.getRequest().getParameter("Nickname");
		String mobile=ServletActionContext.getRequest().getParameter("Mobile");
		String communityhosname=ServletActionContext.getRequest().getParameter("CommunityHosName");
		nickname = new String(nickname.getBytes("ISO-8859-1"), "UTF-8");
		communityhosname = new String(communityhosname.getBytes("ISO-8859-1"), "UTF-8"); 
		
		System.out.println(communityhosname);		
		userid = java.net.URLDecoder.decode(userid,"UTF-8");
		nickname = java.net.URLDecoder.decode(nickname,"UTF-8");
		mobile = java.net.URLDecoder.decode(mobile,"UTF-8");
		communityhosname = java.net.URLDecoder.decode(communityhosname,"UTF-8");
		System.out.println(communityhosname);
		
		JSONObject returnjson=new JSONObject();
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json;charset=utf-8"); 
		try {
			PrintWriter out = response.getWriter();
			int id=Integer.parseInt(userid);			
			user=this.usersservice.getById(id);
			if (user!=null){
				flag=1;
			    userdata=this.userdataservice.getById(id);
			    int communityhosID=user.getCommunityId();
			    community=this.communityservice.getById(communityhosID);
			    user.setNickName(nickname);
			    userdata.setPhoneNumber(mobile);
			    community.setCommunityHosName(communityhosname);
			    this.usersservice.save(user);
			    this.userdataservice.save(userdata);
			    this.communityservice.save(community);
				returnjson.put("flag", flag);
			}else{
				flag=0;
				returnjson.put("flag", flag);
			}						
			out.println(returnjson.toString());
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		// TODO Auto-generated method stub
		this.response=response;
	}

	public Users getUser() {
		return user;
	}

	public void setUser(Users user) {
		this.user = user;
	}

	public Userdata getUserdata() {
		return userdata;
	}

	public void setUserdata(Userdata userdata) {
		this.userdata = userdata;
	}

	public Community getCommunity() {
		return community;
	}

	public void setCommunity(Community community) {
		this.community = community;
	}


}
